package com.orchidea.uml.view.classdiagram
{
import flash.events.MouseEvent;

import mx.binding.utils.BindingUtils;
import mx.containers.VBox;
import mx.core.UIComponent;

public class ElementViewBase extends VBox
{

    public var header : ElementHeader;
    [Bindable]
    public var properties : UIComponent;
    [Bindable]
    public var methods : UIComponent;

    private var _isMoving : Boolean = false;

    protected function onHeaderMouseDown(event:MouseEvent):void
    {
        header.addEventListener(MouseEvent.MOUSE_UP, onHeaderMouseUp);
        header.stage.addEventListener(MouseEvent.MOUSE_MOVE, onHeaderMouseMove);
    }

    protected function onHeaderMouseUp(event:MouseEvent):void
    {
        header.removeEventListener(MouseEvent.MOUSE_UP, onHeaderMouseUp);
        header.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onHeaderMouseMove);

        if(_isMoving)
        {
            this.addEventListener(MouseEvent.CLICK, onHeaderMouseClick, true);
        }

        stopMove();
    }
    protected function onHeaderMouseClick(event:MouseEvent):void
    {
        event.stopPropagation();
        this.removeEventListener(MouseEvent.CLICK, onHeaderMouseClick, true);
    }
    protected function onHeaderMouseMove(event:MouseEvent):void
    {
        if(!_isMoving && event.buttonDown)
        {
            startMove();
        }
    }


    protected function startMove():void
    {
        startDrag(false);
        _isMoving = true;
    }

    protected function stopMove():void
    {
        _isMoving = false;
        stopDrag();
    }



    //-----------------------------
    // open / close
    //-----------------------------
    protected function onHeaderClick(event:MouseEvent):void
    {
        switch(currentState)
        {
            case "open" :
                close();
                break;
            case "closed" :
                open();
                break;
        }
    }

    protected function open():void
    {
        currentState = "open";
    }
    protected function close():void
    {
        currentState = "closed";
    }


    public override function initialize():void
    {
        super.initialize();

        header.addEventListener(MouseEvent.MOUSE_DOWN, onHeaderMouseDown);
        header.addEventListener(MouseEvent.CLICK, onHeaderClick);
    }
}
}